﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace WmsApp.Security
{
    public partial class LoginForm : DevExpress.XtraEditors.XtraForm
    {
        private IUserInfoConfigurator userInfoConfigurator = null;
        public LoginForm()
        {
            InitializeComponent();
            userInfoConfigurator = new UserInfoConfiguratorAppConfig();
            try
            {
                this.checkEditSavePwd.Checked = (userInfoConfigurator.GetCfgValue("SavePwd") == "true");
            }
            catch { }

            try
            {
                this.textEditUserID.Text = userInfoConfigurator.GetUserID();
            }
            catch{}
            try
            {
                if (this.checkEditSavePwd.Checked)
                {
                    this.textEditPassword.Text = userInfoConfigurator.GetPwd();
                }
            }
            catch { }
         
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void CmdOkClick(object sender, EventArgs e)
        {
            if(!UserSession.CheckVersion())
            {
                return;
            }

            string result = UserSession.Login(this.textEditUserID.Text,this.textEditPassword.Text);
            if(UserSession.IsLogOn && result.StartsWith(UserSession.PrixToken))
            {
                try
                {
                    userInfoConfigurator.SaveUserID(this.textEditUserID.Text);
                    if (this.checkEditSavePwd.Checked)
                    {
                        userInfoConfigurator.SavePwd(this.textEditPassword.Text);
                        userInfoConfigurator.SetCfgValue("SavePwd","true");
                    }
                    else
                    {
                        userInfoConfigurator.SavePwd("");
                        userInfoConfigurator.SetCfgValue("SavePwd", "false");
                    }
                }
                catch { }

                if(UserSession.CurrentUser.USERID == UserSession.AdminUser)
                {
                    FormMain.FormMainInstance.UserManagerVisual(true);
                }
                else
                {
                    FormMain.FormMainInstance.UserManagerVisual(false);
                }
                FormMain.FormMainInstance.CloseAllChild();
                FormMain.FormMainInstance.Text = string.Format(FormMain.HeadText,  UserSession.CurrentUser.USERNAME);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                XtraMessageBox.Show(result, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}